import {
  defineStore
} from 'pinia'
import {
  ref
} from 'vue'
import {
  login,
  logout
} from '@/api/login'
export const useUserStore = defineStore('user', () => {
  // 用户信息
  const userInfo = ref({})
  // 访客token
  const viewToken = ref('')
  // 获取用户信息
  const getUserInfo = async (username, password) => {
    const res = await login(username, password)
    // 存储起来
    userInfo.value = res.data
  }
  // 清除用户信息
  const clearUserInfo = async () => {
    await logout()
    clear()
  }
  const clear = () => {
    userInfo.value = {}
    window.localStorage.removeItem('user')
  }

  // 暴露出去
  return {
    userInfo,
    getUserInfo,
    clearUserInfo,
    clear,
    viewToken
  }
}, {
  persist: true
})